﻿using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using PoolGame2.Player;

namespace PoolGame2.Menus
{
    public class ServerMenu : Menu
    {
        MenuItem[] connectionChoices;
        AvailableNetworkSessionCollection availableSessions = null;

        public ServerMenu(PoolGame game)
            : base(game)
        {
            backgroundTexture = game.textureDatabase.GetTexture("background_find");

            connectionChoices = new MenuItem[5];
            float height = 350;

            for (int i = 0; i < 5; i++)
            {
                connectionChoices[i] = new MenuItem("Game: " + i, game);
                connectionChoices[i].Position = new Vector2(550, height);
                height += (connectionChoices[i].GetHeight() * 1.5f);
                connectionChoices[i].onSelected = new EventHandler(joinSelected);
                connectionChoices[i].Visible = false;
                AddMenuItem(connectionChoices[i]);
            }

            MenuItem item = new MenuItem("Exit", game);
            item.Position = new Vector2(40, 60);
            item.onSelected = new EventHandler(exitSelected);
            AddMenuItem(item);
        }

        public override void OnOpen()
        {
            for( int i = 0; i < 5; i++ )
                connectionChoices[i].Visible = false;

            availableSessions = NetworkSession.Find(NetworkSessionType.SystemLink, 2, null);
            int pos = 0;
            foreach (AvailableNetworkSession sess in availableSessions)
            {
                if (pos > 4)
                    break;
                connectionChoices[pos].Visible = true;
                connectionChoices[pos].Text = "Game: " + sess.HostGamertag + " | Ping: " + sess.QualityOfService.MinimumRoundtripTime;
                pos++;
            }

            bool canCheckPlayerMatches = false;
            PoolGame pgame = (PoolGame)Game;
            foreach (PlayerPresence player in pgame.signedInPlayers)
            {
                SignedInGamer gamer = player.GetSignedInGamer();
                if (gamer != null)
                    if (gamer.IsSignedInToLive)
                        canCheckPlayerMatches = true;
            }

            if (pos < 5 && canCheckPlayerMatches)
            {
                availableSessions = NetworkSession.Find(NetworkSessionType.PlayerMatch, 2, null);
                foreach (AvailableNetworkSession sess in availableSessions)
                {
                    if (pos > 4)
                        break;

                    connectionChoices[pos].Visible = true;
                    connectionChoices[pos].Text = "Game: " + sess.HostGamertag + " | Ping: " + sess.QualityOfService.MinimumRoundtripTime;
                    pos++;
                }
            }
            base.OnOpen();
        }

        public override void Update(GameTime gameTime)
        {
            int pos = 0;
            foreach (AvailableNetworkSession sess in availableSessions)
            {
                connectionChoices[pos].Visible = true;
                connectionChoices[pos].Text = "Game: " + sess.HostGamertag + " | Ping: " + sess.QualityOfService.MinimumRoundtripTime;
                pos++;
            }
            base.Update(gameTime);
        }

        public void joinSelected(object caller, EventArgs e)
        {
            for (int i = 0; i < 5; i++)
            {
                if (connectionChoices[i] == caller)
                {
                    PoolGame pgame = (PoolGame)Game;
                    pgame.networkManager.JoinSession(availableSessions[i]);
                    return;
                }
            }
        }

        public void exitSelected(object caller, EventArgs e)
        {
            ((PoolGame)(Game)).menuManager.PreviousMenu();
        }
    }
}
